//
//  HoneView.m
//  Feel_at_home
//
//  Created by dlios on 15/9/23.
//  Copyright © 2015年 Feel_at_home. All rights reserved.
//

#import "HoneView.h"
#import "HoneHeadCell.h"
#define w self.frame.size.width

@interface HoneView ()<UICollectionViewDelegate,UICollectionViewDataSource>

@property (nonatomic,strong) UICollectionView *collection;
@property (nonatomic,strong) UICollectionViewFlowLayout *flowlayout;

@end

@implementation HoneView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        CGFloat h = self.frame.size.height;
        
        self.flowlayout = [[UICollectionViewFlowLayout alloc]init];
        self.flowlayout.itemSize = CGSizeMake(w, 170);
        self.flowlayout.minimumLineSpacing = 0;
        self.flowlayout.minimumInteritemSpacing = 0;
        self.flowlayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        
        self.collection = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, w, h) collectionViewLayout:self.flowlayout];
        
        self.collection.contentSize = CGSizeMake(w * (self.picarr.count+1), 0);

        /* 两个协议 */
        self.collection.delegate = self;
        self.collection.dataSource = self;
        /* 背景默认为黑色 (改为白色)*/
        self.collection.backgroundColor = [UIColor whiteColor];
        self.collection.pagingEnabled = YES;/* 翻页 */
        self.collection.showsHorizontalScrollIndicator = NO;/* 开启指示器图像下边滑条 */
        [self addSubview:self.collection];
        //collectionview cell 必须提前注册
        [self.collection registerClass:[HoneHeadCell class] forCellWithReuseIdentifier:@"reuse"];
        
        self.collection.contentOffset = CGPointMake(w, 0);
        
    }
    
    return self;
}

//#pragma mark - 轮播图 collectionView
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    HoneHeadCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"reuse" forIndexPath:indexPath];
    
    [cell homeData:self.picarr [indexPath.row]];
    return cell;
}
- (void)layoutSubviews{
    [super layoutSubviews];
    [self nsTimer];
    
}
- (void)nsTimer
{
    self.mytimer = [NSTimer scheduledTimerWithTimeInterval:5.f target:self selector:@selector(scrollCollectionView) userInfo:nil repeats:YES];
}
- (void)scrollCollectionView
{
    [self.collection setContentOffset:CGPointMake(self.collection.contentOffset.x + w, 0) animated:NO];
    CATransition *transtion = [CATransition animation];
    transtion.type = @"rippleEffect";
    transtion.duration = 5.f;
    [self.collection.layer addAnimation:transtion forKey:@"ee"];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.picarr.count;
}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1000000;
}
#pragma mark - scrollView方法
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    NSInteger num = (NSInteger)(scrollView.contentOffset.x / w);
    
    [self.collection setContentOffset:CGPointMake(w * 25 * self.picarr.count + (w * (num % self.picarr.count)), 0) animated:NO];
}

- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView
{
    NSInteger num = (NSInteger)(scrollView.contentOffset.x / w);
    [self.collection setContentOffset:CGPointMake(w * 25 * self.picarr.count + (w * (num % self.picarr.count)), 0) animated:NO];
}


@end
